//
//  ShellRegionView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/3.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellRegionView.h"
#import "ShellHomeViewModel.h"
#import "ShellNetworkCalculateViewController.h"
#import "ShellCountryDataModel.h"

@interface  ShellRegionView()

@property (nonatomic,strong) ShellHomeViewModel *viewModel;

//洲
@property (nonatomic,strong) NSMutableArray *continentBtns;

//算力
@property (nonatomic,strong) UILabel *calculateNumLabel;

@end

@implementation ShellRegionView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellHomeViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    
    ///美洲
    UIButton *americaBtn = [UIButton lz_buttonImageName:@"icon_meizhou" backgroundImageName:@""];
    [americaBtn addTarget:self action:@selector(continentClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:americaBtn];
    [americaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(4);
        make.width.mas_equalTo(158);
        make.height.mas_equalTo(180);
        make.top.mas_equalTo(0);
    }];
    
    
    
    ///欧洲
     UIButton *europeBtn = [UIButton lz_buttonImageName:@"icon_ouzhou" backgroundImageName:@""];
     [europeBtn addTarget:self action:@selector(continentClick:) forControlEvents:UIControlEventTouchUpInside];
     [self addSubview:europeBtn];
     [europeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(151);
         make.width.mas_equalTo(219);
         make.height.mas_equalTo(99);
         make.top.mas_equalTo(4);
     }];
    
    
    ///非洲
    UIButton *africaBtn = [UIButton lz_buttonImageName:@"icon_feizhou" backgroundImageName:@""];
    [africaBtn addTarget:self action:@selector(continentClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:africaBtn];
    [africaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(158);
        make.width.mas_equalTo(69);
        make.height.mas_equalTo(77);
        make.top.mas_equalTo(77);
    }];
    
    
    ///亚洲
    UIButton *asiaBtn = [UIButton lz_buttonImageName:@"icon_yazhou" backgroundImageName:@""];
    [self addSubview:asiaBtn];
    [asiaBtn addTarget:self action:@selector(continentClick:) forControlEvents:UIControlEventTouchUpInside];
    [asiaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.mas_equalTo(222);
       make.width.mas_equalTo(109);
       make.height.mas_equalTo(73);
       make.top.mas_equalTo(54);
    }];
    
    
    ///洋州
    UIButton *ceaniaBtn = [UIButton lz_buttonImageName:@"icon_dayangzhou" backgroundImageName:@""];
    [self addSubview:ceaniaBtn];
    [ceaniaBtn addTarget:self action:@selector(continentClick:) forControlEvents:UIControlEventTouchUpInside];
    [ceaniaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(291);
      make.width.mas_equalTo(67);
      make.height.mas_equalTo(41);
      make.top.mas_equalTo(127);
    }];
    
    
    
    
    [self.continentBtns addObject:europeBtn];
    [self.continentBtns addObject:americaBtn];
    [self.continentBtns addObject:asiaBtn];
    [self.continentBtns addObject:ceaniaBtn];
    [self.continentBtns addObject:africaBtn];
    
    [self addSubview:self.calculateNumLabel];
    [self.calculateNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(20);
        make.bottom.mas_equalTo(-10);
    }];
    self.calculateNumLabel.text = @"0";
    
    
    
    UIButton *calculateBtn = [UIButton lz_buttonTitle:[NSString stringWithFormat:@"%@(UG)",LZLocalizedString(@"全网算力")] titleColor:UIColorHex(0x156097) fontSize:12];
    [self addSubview:calculateBtn];
    [calculateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.calculateNumLabel.mas_left).mas_offset(-10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.calculateNumLabel.mas_centerY);
    }];
    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(230,291,7,7);
    view.layer.backgroundColor = [UIColor colorWithRed:74/255.0 green:178/255.0 blue:231/255.0 alpha:1.0].CGColor;
    view.layer.shadowColor = [UIColor colorWithRed:12/255.0 green:34/255.0 blue:191/255.0 alpha:0.7].CGColor;
    view.layer.shadowOffset = CGSizeMake(2,2);
    view.layer.shadowOpacity = 1;
    view.layer.shadowRadius = 4;
    [self addSubview:view];
    view.backgroundColor = UIColorHex(0x4AB2E7);
    view.cornerRadius = 3.5;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(calculateBtn.mas_left).mas_offset(-10);
        make.width.mas_equalTo(7);
        make.height.mas_equalTo(7);
        make.centerY.mas_equalTo(self.calculateNumLabel.mas_centerY);
    }];
    
    
    [self.calculateNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
    }];
    
    CGFloat calculateW = [calculateBtn.currentTitle lz_textWidthWithFontSize:calculateBtn.titleLabel.font withMaxHeight:20];
    [calculateBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(calculateW);
    }];
    
    [[calculateBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        //全网算力图
        ShellNetworkCalculateViewController *calculateVC = [[ShellNetworkCalculateViewController alloc] init];
        [LZTool.currentViewController.navigationController pushViewController:calculateVC animated:YES];
    }];
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self refreshData];
    }];
    
    [self refreshData];

    
    //翻译
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        [calculateBtn setTitle:[NSString stringWithFormat:@"%@(UG)",LZLocalizedString(@"全网算力")] forState:UIControlStateNormal];
        CGFloat calculateW = [calculateBtn.currentTitle lz_textWidthWithFontSize:calculateBtn.titleLabel.font withMaxHeight:20];
        
        [calculateBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(calculateW);
        }];
    }];
    
}

///刷新数据
- (void)refreshData{
    self.calculateNumLabel.text = [NSString stringWithFormat:@"%f",self.viewModel.hashRateModel.hashRate].saveDecimal;
    CGFloat calculateNumW = [self.calculateNumLabel.text lz_textWidthWithFontSize:self.calculateNumLabel.font withMaxHeight:20];
    [self.calculateNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(calculateNumW);
    }];
    
    for (int i = 0; i < self.viewModel.countryArray.count; i++) {
        UIButton *contientBtn = self.continentBtns[i];
        ShellCountryDataModel *dataModel = self.viewModel.countryArray[i];
        [contientBtn setImage:k_imageName(dataModel.imageName) forState:UIControlStateNormal];
    }
//    userCount    注册用户数    int        否    通过注册用户数来显示颜色，配设计稿上的6种颜色
//    lv0: 0-100
//    lv1：100-2999
//    lv2：3000-7999
//    lv3：8000-19999
//    lv4：20000-49999
//    lv5：50000+
    
    
    
    
    
    
    
    
}


//点击地图
- (void)continentClick:(UIButton *)sender{
    
    [LZToolView showAlertType:LZAlertViewTypMapDetail withTitle:@"" message:@"" cancel:@"" sure:@"" objectDict:@{@"continent":self.viewModel.countryArray?:@[]} cancelBlock:^(id  _Nullable object) { } sureBlock:^(id  _Nullable object) {
        
        
        NSInteger index = [object intValue];
        [LZToolView dismiss];
        if(self.viewModel.countryArray.count > index){
            ShellCountryDataModel *countryDataModel = self.viewModel.countryArray[index];
             NSString *orderAmount = [NSString stringWithFormat:@"%f",countryDataModel.orderAmount].saveDecimal;
             NSArray *dataArray = @[[NSString stringWithFormat:@"%@ %ld",LZLocalizedString(@"注册用户"),(long)countryDataModel.userCount],
                                   [NSString stringWithFormat:@"%@ %ld",LZLocalizedString(@"累计完成订单数"),(long)countryDataModel.orderCount],
                                   [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"累计完成订单金额"),orderAmount],
                                   [NSString stringWithFormat:@"%@ %ld",LZLocalizedString(@"超级节点"),(long)countryDataModel.superNodeCount]];

             
            [LZToolView showAlertType:LZAlertViewTypContinentDetail withTitle:countryDataModel.name message:@"" cancel:@"" sure:@"" objectDict:@{@"dataArray":dataArray} cancelBlock:^(id  _Nullable object) { } sureBlock:^(id  _Nullable object) { }];
        }
        
    }];
}


//算力
- (UILabel *)calculateNumLabel{
    if(!_calculateNumLabel){
        _calculateNumLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(13) color:UIColorHex(0x4AB2E7)];
        [_calculateNumLabel lz_rightAlignment];
    }
    return _calculateNumLabel;
}

- (NSMutableArray *)continentBtns{
    if (!_continentBtns) {
        _continentBtns = [[NSMutableArray alloc] init];
    }
    return _continentBtns;
}
@end
